#include "userpage.h"
#include "ui_userpage.h"
#include <QInputDialog>
#include <QGraphicsDropShadowEffect>
#include "util/messagebox.h"
#include <QDateTime>

namespace WordMemory
{
    UserPage::UserPage(QWidget *parent)
        : QWidget(parent)
        , ui(new Ui::UserPage)
        , serverhelper(new ServerHelper(this))
    {
        ui->setupUi(this);
        applyStyles();
        setupConnections();
        loadUserInfo();
    }

    UserPage::~UserPage()
    {
        delete ui;
    }

    void UserPage::refreshPageData() {
        loadUserInfo();
    }

    void UserPage::applyStyles() {
        // Shadow effect for the card
        QGraphicsDropShadowEffect *cardShadow = new QGraphicsDropShadowEffect(this);
        cardShadow->setBlurRadius(15);
        cardShadow->setXOffset(0);
        cardShadow->setYOffset(3);
        cardShadow->setColor(QColor(0, 0, 0, 60));
        ui->infoCardFrame->setGraphicsEffect(cardShadow);
    }

    void UserPage::setupConnections()
    {

        connect(serverhelper, &ServerHelper::getUserFinished, this, &UserPage::onGetUserFinished);
        connect(serverhelper, &ServerHelper::updateUserFinished, this, &UserPage::onUpdateUserFinished);
    }

    void UserPage::loadUserInfo()
    {
        if (!UserManager::instance().isLoggedIn()) {

            ui->usernameLabel->setText("请先登录");
            ui->realNameLabel->setText("-");
            ui->createdAtLabel->setText("-");
            ui->updateButton->setEnabled(false);
            return;
        }
        ui->updateButton->setEnabled(true);
        int userId = UserManager::instance().getUserId();
        serverhelper->getUser(userId);
    }

    void UserPage::onGetUserFinished(bool success, const User &user, const QString &error) {
        if (success) {
            ui->usernameLabel->setText(user._username);
            ui->realNameLabel->setText(user._realName.isEmpty() ? "未设置" : user._realName);


            QDateTime createdAtDateTime = QDateTime::fromString(user._createdAt, Qt::ISODate);
            if (!createdAtDateTime.isValid()) {
                 createdAtDateTime = QDateTime::fromString(user._createdAt, "yyyy-MM-dd HH:mm:ss");
            }
            ui->createdAtLabel->setText(createdAtDateTime.isValid() ? createdAtDateTime.toString("yyyy年MM月dd日") : "未知");

        } else {
            MessageBox::warning(this, "加载用户信息失败: " + error);
            ui->usernameLabel->setText("加载失败");
            ui->realNameLabel->setText("加载失败");
            ui->createdAtLabel->setText("加载失败");
        }
    }

    void UserPage::onUpdateUserFinished(bool success, const User &user, const QString &error) {
        if (success) {
            MessageBox::info(this, "用户信息已更新");
            UserManager::instance().setCurrentUser(user);
            loadUserInfo();
        } else {
            MessageBox::warning(this, "更新用户信息失败: " + error);
        }
    }


    void UserPage::on_updateButton_clicked()
    {
        if (!UserManager::instance().isLoggedIn()) {
            MessageBox::warning(this, "请先登录");
            return;
        }

        bool ok;
        QString currentRealName = UserManager::instance().getRealName();
        QString newRealName = QInputDialog::getText(this, "更新真实姓名",
                                                 "请输入新的真实姓名：", QLineEdit::Normal,
                                                 currentRealName, &ok);
        if (ok) {
            if (newRealName != currentRealName) {
                int userId = UserManager::instance().getUserId();
                serverhelper->updateUser(userId, newRealName);
            }
        }
    }
}
